package com.example.administrator.myapplication.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.CourseModel;
import com.example.administrator.myapplication.activity.LoginModel;
import com.example.administrator.myapplication.activity.RegisterActivity;
import com.example.administrator.myapplication.activity.VideoActivity;
import com.example.administrator.myapplication.activity.loginActivity;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.logging.Handler;
import java.util.logging.LogRecord;


public class CourseFragment extends Fragment {
    RecyclerView recyclerView;
    ViewPager viewPager;
    int curentpostion = 0;
    public ArrayList<ImageView> viewPagerImgs = new ArrayList<>();
    public ArrayList<CourseModel.Course> courselists = new ArrayList<>();
     android.os.Handler handler=new android.os.Handler(new android.os.Handler.Callback() {
         @Override
         public boolean handleMessage(Message message) {
             Log.i("CourseFramgent", "handleMessage: "+message.what);
             handler.sendEmptyMessageDelayed(1,1000);
             curentpostion++;
             viewPager.setCurrentItem(curentpostion % viewPagerImgs.size());
             return false;
         }
     });



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        loading();

        View view = inflater.inflate(R.layout.fragment_course, container, false);

        recyclerView = view.findViewById(R.id.recycler);
        viewPager = view.findViewById(R.id.viewpager);
        return view;
    }

    public void loading() {
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.Course.Getcourse");

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("loginActivity", "onSuccess result" + result);
                Gson gson = new Gson();
                CourseModel courseModel = gson.fromJson(result, CourseModel.class);
                if (courseModel != null && courseModel.data != null && courseModel.data.courselist != null) {
                    courselists = courseModel.data.courselist;
                }
                GridLayoutManager glm = new GridLayoutManager(getActivity(), 2);
                recyclerView.setLayoutManager(glm);
                recyclerView.setAdapter(new MyAdapter());
                ArrayList<String> banner = courseModel.data.banner;
                ImageView imageView1=new ImageView(getActivity());
                x.image().bind(imageView1,banner.get(banner.size()-1));
                viewPagerImgs.add(imageView1);
                for (int i = 0; i < banner.size(); i++) {

                    ImageView imageView = new ImageView(getActivity());
                    x.image().bind(imageView, banner.get(i));
                    viewPagerImgs.add((imageView));
                }
                ImageView imageView2=new ImageView(getActivity());
                x.image().bind(imageView2,banner.get(0));
                viewPagerImgs.add(imageView2);
               viewPager.setAdapter(new MyViewPagerAdapter());
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
                   int  curentpostion;
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                         curentpostion = position;
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                        if (state!=ViewPager.SCROLL_STATE_IDLE){
                            return;
                        }
                        if(curentpostion==0){
                            viewPager.setCurrentItem(viewPagerImgs.size()-2,false);
                        }else if (curentpostion==viewPagerImgs.size()-1){
                            viewPager.setCurrentItem(1,false);
                        }
                    }
                });
                viewPager.setCurrentItem(1);
                Log.i("CourseFramgent", "sendEmptyMessageDelayed ");
                handler.sendEmptyMessageDelayed(1,2000);
                Log.i("loginActivity", "result "+result);
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView tv;
        TextView contentTitleTv;
        LinearLayout videoView;

        public MyViewHolder(View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.img_item);
            tv = itemView.findViewById(R.id.title);
            contentTitleTv = itemView.findViewById(R.id.contentitle);
            videoView = itemView.findViewById(R.id.videoView);
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(), R.layout.item_course, null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CourseModel.Course course = courselists.get(position);
            holder.tv.setText(course.title);
            holder.contentTitleTv.setText(course.titlecontent);
            x.image().bind(holder.iv, course.icon);
            holder.videoView.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(getActivity(), VideoActivity.class);
                    intent.putExtra("imaurl", course.imgurl);
                    intent.putExtra("des", course.des);
                    intent.putExtra("video", course.videourl);
                    startActivity(intent);
                }
            });

        }

        @Override
        public int getItemCount() {
            return courselists.size();
        }
    }

    public class MyViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return viewPagerImgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = viewPagerImgs.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewPagerImgs.get(position));

        }

    }
}
